package com.ling.pl.security.view.frame.main.register.onlineuser;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.common.event.DefaultClientEvent;
import com.bstek.dorado.core.resource.ResourceManager;
import com.bstek.dorado.core.resource.ResourceManagerUtils;
import com.bstek.dorado.view.widget.Container;
import com.bstek.dorado.view.widget.SubViewHolder;
import com.bstek.dorado.view.widget.form.Label;
import com.ling.pl.security.feign.DefaultUserService;
import com.ling.pl.security.view.frame.main.IFrameShortcutActionRegister;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bo.wang
 */
@Component("ling2.onlineUserShortcutRegister")
public class OnlineUserShortcutRegister implements IFrameShortcutActionRegister {
    private static final ResourceManager resourceManager = ResourceManagerUtils
            .get(OnlineUserShortcutRegister.class);

    @Resource
    DefaultUserService userService;

    @Value("${ling2.disabledOnlineUserShortcutRegister:false}")
    private boolean disabledOnlineUserShortcutRegister;

    @Expose
    public int getLoginuserCount() {
        return userService.getLoginuserCount();
    }

    @DataProvider
    public List<Map<String, Object>> loadAllLoginUsers() {
        return userService.loadAllLoginUsers();
    }


    public void registerToStatusBar(Container container) {
        SubViewHolder subviewHolder = new SubViewHolder();
        subviewHolder.setSubView("security.view.frame.main.register.onlineuser.OnlineUser");
        container.addChild(subviewHolder);

        Label onlineUserCountLabel = new Label();
        onlineUserCountLabel.setId("onlineUserCountLabel");
        onlineUserCountLabel.setTip(resourceManager.getString("bdf2.core/loginUserCountLabelTip"));
        Map<String, Object> style = new HashMap<String, Object>();
        style.put("cursor", "pointer");
        style.put("padding-left", "5px");
        onlineUserCountLabel.setStyle(style);
        onlineUserCountLabel.addClientEventListener("onRefreshDom", new DefaultClientEvent("arg.dom.style.lineHeight=arg.dom.style.height;"));
        onlineUserCountLabel.addClientEventListener("onDoubleClick", new DefaultClientEvent("window.showOnlineUserDialog()"));
        onlineUserCountLabel.setText(resourceManager.getString("bdf2.core/loginUserCountLabel", userService.getLoginuserCount()));
        container.addChild(onlineUserCountLabel);
    }

    public void registerToFrameTop(Container container) {

    }

    public int order() {
        return 2;
    }

    public boolean isDisabled() {
        return disabledOnlineUserShortcutRegister;
    }
}
